﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.HVAC.LevelIndependentTag;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class PipeDiameterMarkOperation
	{
		public bool UseForVrvLN { get; set; }

		public PipeDiameterMarkOperation(ExternalCommandData externalCommanData, FormPipeDiameterMark formMark)
		{
			this.UseForVrvLN = false;
			this.commandData = externalCommanData;
			this.diameterMark = formMark;
			this.uiDoc = this.commandData.Application.ActiveUIDocument;
			this.doc = this.uiDoc.Document;
			this.selection = this.uiDoc.Selection;
			this.userSetting = new UserSetting();
			this.SetDynamicDefaultValue();
		}

		public bool Execute(bool bePlane)
		{
			bool result = true;
			Transaction transaction = null;
			try
			{
				for (;;)
				{
					transaction = new Transaction(this.doc, "管径标注");
					transaction.Start();
					MarkOperation markOperation = new MarkOperation();
					this.commonFont = this.userSetting.GetFontSetting();
					markOperation.SetDiameterAccuracy(this.doc);
					this.allDetailCurve = new ElementSet();
					DiameterMarkWay markWay = this.diameterMark.GetMarkWay();
					XYZ xyz = null;
					switch (markWay)
					{
					case DiameterMarkWay.MuiltyDown:
					case DiameterMarkWay.MuiltyUp:
						this.PickRectanglePipes(bePlane);
						break;
					case DiameterMarkWay.MultyLine:
						this.SelectPipes(ref xyz);
						break;
					case DiameterMarkWay.SingleDown:
					case DiameterMarkWay.SingleUp:
						this.PickSinglePipe(ref xyz, bePlane, false);
						break;
					case DiameterMarkWay.SingleLine:
						this.PickSinglePipe(ref xyz, bePlane, true);
						break;
					}
					this.CreatGroupLine(this.allDetailCurve, this.doc);
					transaction.Commit();
					markOperation.SetActiveDocAllLoadedTags(this.doc, false);
				}
			}
			catch (Exception ex)
			{
				if (transaction != null && (int)transaction.GetStatus() == 1)
				{
					transaction.Commit();
				}
				ex.ToString();
			}
			finally
			{
				this.diameterMark.Close();
			}
			return result;
		}

		private void SetPipeParameter(Pipe pipe, XYZ globalPoint)
		{
			SubTransaction subTransaction = new SubTransaction(pipe.Document);
			try
			{
				subTransaction.Start();
				this.SetDiameterPrefix(pipe);
				this.SetDiameterThickess(pipe);
				this.SetLevelFix(pipe, globalPoint);
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				ex.ToString();
				if (subTransaction != null && (int)subTransaction.GetStatus() == 1)
				{
					subTransaction.RollBack();
				}
			}
		}

		private void SetDiameterPrefix(Pipe pipe)
		{
			string pipePrefix = this.GetPipePrefix(pipe);
			PipeDiameterMarkOperation.MarkDiameterType prefixType = this.GetPrefixType(pipePrefix);
			string paramName;
			if (prefixType != PipeDiameterMarkOperation.MarkDiameterType.OutDiameter)
			{
				if (prefixType != PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter)
				{
					paramName = "直径前缀";
				}
				else
				{
					paramName = "内径前缀";
				}
			}
			else
			{
				paramName = "外径前缀";
			}
			pipe.GetParameter(paramName).Set(pipePrefix);
		}

		private void SetDiameterThickess(Pipe pipe)
		{
			this.userSetting = new UserSetting();
			PipeDiameterMarkSet diameterMarkSetting = this.userSetting.GetDiameterMarkSetting();
			if (!this.CheckStringBeTrue(diameterMarkSetting.Thickness))
			{
				return;
			}
			double num = (this.GetOutDiameter(pipe) - this.GetInnerDiameter(pipe)) / 2000.0;
			pipe.GetParameter("壁厚").Set(num);
			Parameter parameter = pipe.GetParameter("连字符");
			string text = " ";
			if (diameterMarkSetting.Splite != "空格")
			{
				text = diameterMarkSetting.Splite;
			}
			parameter.Set(text);
		}

		private void SetLevelFix(Pipe pipe, XYZ globalPoint)
		{
			this.userSetting = new UserSetting();
			PipeDiameterMarkSet diameterMarkSetting = this.userSetting.GetDiameterMarkSetting();
			if (!this.CheckStringBeTrue(diameterMarkSetting.PipeOffset))
			{
				return;
			}
			PipeOffsetMarkSet offsetMarkSetting = this.userSetting.GetOffsetMarkSetting();
			string splite = offsetMarkSetting.Splite;
			if (splite.Equals("空格") || this.StringBeBrackets(splite))
			{
				Parameter parameter = pipe.GetParameter("分隔符");
				parameter.Set(" ");
				if (!this.StringBeBrackets(splite))
				{
					parameter = pipe.GetParameter("标高后缀");
					parameter.Set(" ");
				}
			}
			else
			{
				Parameter parameter = pipe.GetParameter("分隔符");
				parameter.Set(splite);
				if (!this.StringBeBrackets(splite))
				{
					parameter = pipe.GetParameter("标高后缀");
					parameter.Set(" ");
				}
			}
			if (!offsetMarkSetting.Suffix.Equals("无"))
			{
				Parameter parameter = pipe.GetParameter("标高后缀");
				if (this.StringBeBrackets(splite))
				{
					if (this.JudgeBeShow(offsetMarkSetting.UnitVisible))
					{
						parameter.Set(offsetMarkSetting.Suffix + ")");
					}
					else
					{
						parameter.Set(")");
					}
					if (!offsetMarkSetting.Content.Equals("相对标高"))
					{
						parameter = pipe.GetParameter("分隔符");
						parameter.Set("(");
					}
				}
				else if (this.JudgeBeShow(offsetMarkSetting.UnitVisible))
				{
					parameter.Set(offsetMarkSetting.Suffix);
				}
			}
			if (offsetMarkSetting.Content.Equals("相对标高"))
			{
				string text = "";
				double levelOffset = pipe.LevelOffset;
				if (!offsetMarkSetting.Prefix.Equals("无"))
				{
					text = offsetMarkSetting.Prefix;
					if (levelOffset > 0.0)
					{
						text += "+";
					}
				}
				if (this.StringBeBrackets(splite))
				{
					text = "(" + text;
				}
				Parameter parameter = pipe.GetParameter("标高前缀");
				parameter.Set(text);
			}
			if (offsetMarkSetting.Content.Equals("绝对标高") && globalPoint != null)
			{
				this.SetObsoluteLevel(pipe, globalPoint);
			}
		}

		private bool JudgeBeShow(string visibleText)
		{
			return visibleText.Equals("显示");
		}

		private bool StringBeBrackets(string strSplite)
		{
			return strSplite.Equals("()") || strSplite.Equals("（）");
		}

		private List<Connector> GetReduceConn(Pipe pipe)
		{
			List<Connector> list = new List<Connector>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			Connector connector = hypiping.StartConnector.Connector;
			Connector connector2 = hypiping.EndConnector.Connector;
			if (this.ConnElemBeReduce(connector))
			{
				list.Add(connector);
			}
			if (this.ConnElemBeReduce(connector2))
			{
				list.Add(connector2);
			}
			return list;
		}

		private bool ConnElemBeReduce(Connector conn)
		{
			Connector connectConnector = YJKRevitTools.GetConnectConnector(conn);
			if (connectConnector == null)
			{
				return false;
			}
			Element owner = connectConnector.Owner;
			if (owner is FamilyInstance)
			{
				FamilyInstance familyInstance = owner as FamilyInstance;
				MechanicalFitting mechanicalFitting = familyInstance.MEPModel as MechanicalFitting;
				if (mechanicalFitting == null)
				{
					return false;
				}
				if ((int)mechanicalFitting.PartType == 7)
				{
					return true;
				}
				foreach (Connector connector in YJKRevitTools.GetElementAllConnector(familyInstance))
				{
					connectConnector = YJKRevitTools.GetConnectConnector(connector);
					if (connectConnector != null)
					{
						owner = connectConnector.Owner;
						if (owner is FamilyInstance)
						{
							familyInstance = (owner as FamilyInstance);
							mechanicalFitting = (familyInstance.MEPModel as MechanicalFitting);
							if (mechanicalFitting != null && (int)mechanicalFitting.PartType == 7)
							{
								return true;
							}
						}
					}
				}
				return false;
			}
			return false;
		}

		private List<XYZ> GetPipeIntersetWallPoint(Pipe pipe)
		{
			List<XYZ> list = new List<XYZ>();
			List<Document> linkDocs = this.GetLinkDocs(this.doc);
			List<Wall> list2 = this.GetIntersectWallByPipe(pipe, this.doc);
			if (linkDocs != null && linkDocs.Count > 0)
			{
				foreach (Autodesk.Revit.DB.Document document in linkDocs)
				{
					if (list2 == null)
					{
						list2 = new List<Wall>();
					}
					List<Wall> intersectWallByPipe = this.GetIntersectWallByPipe(pipe, document);
					if (intersectWallByPipe != null && intersectWallByPipe.Count > 0)
					{
						list2.AddRange(intersectWallByPipe);
					}
				}
			}
			if (list2 == null || list2.Count < 1)
			{
				return null;
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			Line line = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin).Line;
			XYZ direction = line.Direction;
			XYZ.BasisX.AngleOnPlaneTo(direction, XYZ.BasisZ);
			foreach (Wall wall in list2)
			{
				if ((int)wall.WallType.Kind != 2 && (int)wall.WallType.Kind != 1 && !((wall.Location as LocationCurve).Curve.GetType() == typeof(Arc)) && Math.Abs(this.FindWallVector(wall).AngleTo(direction) - Math.PI*.5) <= 0.0001)
				{
					AssistFunc.mmToFeet(100.0);
					double width = wall.WallType.Width;
					List<XYZ> cvIntersect2WallPoints = this.GetCvIntersect2WallPoints(line, wall);
					if (cvIntersect2WallPoints != null && cvIntersect2WallPoints.Count > 0)
					{
						list.AddRange(cvIntersect2WallPoints);
					}
				}
			}
			return list;
		}

		private XYZ FindWallVector(Wall wall)
		{
			LocationCurve locationCurve = wall.Location as LocationCurve;
			return locationCurve.Curve.GetEndPoint(1) - locationCurve.Curve.GetEndPoint(0);
		}

		private List<XYZ> GetCvIntersect2WallPoints(Curve cv, Wall wall)
		{
			List<XYZ> list = new List<XYZ>();
			try
			{
				XYZ endPoint = cv.GetEndPoint(0);
				XYZ endPoint2 = cv.GetEndPoint(1);
				XYZ zero = XYZ.Zero;
				XYZ zero2 = XYZ.Zero;
				if (this.GetElemLocationCurve(wall, ref zero, ref zero2))
				{
					double z = zero.Z;
					XYZ xyz = new XYZ(endPoint.X, endPoint.Y, z);
					XYZ xyz2 = new XYZ(endPoint2.X, endPoint2.Y, z);
					Curve curve = Line.CreateBound(xyz, xyz2);
					IntersectionResultArray intersectionResultArray = null;
					Curve elemLocationCurve = this.GetElemLocationCurve(wall);
					if ((int)curve.Intersect(elemLocationCurve, out intersectionResultArray) == 8)
					{
						for (int i = 0; i < intersectionResultArray.Size; i++)
						{
							XYZ xyzpoint = intersectionResultArray.get_Item(i).XYZPoint;
							Curve curve2 = Line.CreateUnbound(xyzpoint, XYZ.BasisZ);
							IntersectionResultArray intersectionResultArray2 = null;
							if ((int)curve2.Intersect(cv, out intersectionResultArray2) == 8 && intersectionResultArray2.Size > 0)
							{
								xyzpoint = intersectionResultArray2.get_Item(0).XYZPoint;
								list.Add(xyzpoint);
							}
						}
					}
				}
			}
			catch
			{
			}
			return list;
		}

		private bool GetElemLocationCurve(Element elem, ref XYZ ptStart, ref XYZ ptEnd)
		{
			Curve elemLocationCurve = this.GetElemLocationCurve(elem);
			if (elemLocationCurve != null)
			{
				ptStart = elemLocationCurve.GetEndPoint(0);
				ptEnd = elemLocationCurve.GetEndPoint(1);
				return true;
			}
			return false;
		}

		private Curve GetElemLocationCurve(Element elem)
		{
			if (elem == null)
			{
				return null;
			}
			LocationCurve locationCurve = elem.Location as LocationCurve;
			if (locationCurve == null)
			{
				return null;
			}
			return locationCurve.Curve;
		}

		private List<Wall> GetIntersectWallByPipe(MEPCurve elemPipe, Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(Wall));
			ElementIntersectsElementFilter elementIntersectsElementFilter = new ElementIntersectsElementFilter(elemPipe);
			filteredElementCollector.WherePasses(elementIntersectsElementFilter);
			return filteredElementCollector.Cast<Wall>().ToList<Wall>();
		}

		private List<Document> GetLinkDocs(Autodesk.Revit.DB.Document doc)
		{
			List<Document> list = new List<Document>();
			try
			{
				foreach (RevitLinkInstance revitLinkInstance in ((IEnumerable<RevitLinkInstance>)new FilteredElementCollector(doc).OfClass(typeof(RevitLinkInstance)).Cast<RevitLinkInstance>().ToList<RevitLinkInstance>()))
				{
					Document linkDocument = revitLinkInstance.GetLinkDocument();
					if (linkDocument != null && !string.IsNullOrEmpty(linkDocument.PathName))
					{
						list.Add(linkDocument);
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return list;
		}

		private void SetObsoluteLevel(Pipe pipe, XYZ globalPoint)
		{
			double num = pipe.ReferenceLevel.GetParameter(BuiltInParameter.LEVEL_ELEV).AsDouble();
			double levelOffset = pipe.LevelOffset;
			double num2 = num + levelOffset;
			double num3 = pipe.GetParameter(BuiltInParameter.RBS_START_OFFSET_PARAM).AsDouble();
			double num4 = pipe.GetParameter(BuiltInParameter.RBS_END_OFFSET_PARAM).AsDouble();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			XYZ project = hyline.GetProject(globalPoint);
			if (!project.IsAlmostEqualTo(hyline.StartPoint, 1E-05))
			{
				YJKLine hyline2 = new YJKLine(hyline.StartPoint, project);
				num2 = num + num3 + hyline2.Line.Length / hyline.Line.Length * (num4 - num3);
			}
			else
			{
				num2 = num + num3;
			}
			Parameter parameter = pipe.GetParameter("绝对标高");
			parameter.Set(num2);
			parameter = pipe.GetParameter("标高前缀");
			if (!string.IsNullOrEmpty(parameter.AsString()))
			{
				return;
			}
			if (Math.Abs(Math.Round(num2, 7)) <= 1E-07)
			{
				parameter.Set("±");
			}
			if (Math.Round(num2, 7) > 1E-07)
			{
				parameter.Set(" ");
			}
			if (Math.Round(num2, 7) < -1E-07)
			{
				parameter.Set(" ");
			}
		}

		private void DrawSingleSlashLine(Autodesk.Revit.DB.Document doc, XYZ basePoint, XYZ pickPoint, XYZ hVector)
		{
			SubTransaction subTransaction = new SubTransaction(doc);
			try
			{
				basePoint = YJKRevitTools.SetZToZero(basePoint);
				pickPoint = YJKRevitTools.SetZToZero(pickPoint);
				XYZ xyz = pickPoint.Subtract(basePoint);
				XYZ xyz2 = new XYZ(-xyz.Y, xyz.X, 0.0).Normalize();
				if (this.JudgePointQuadrant(xyz.Normalize()))
				{
					xyz2 = -xyz2;
				}
				double num = pickPoint.DistanceTo(basePoint);
				double num2 = Math.Tan(0.26179938779914941);
				double num3 = num * num2;
				XYZ xyz3 = pickPoint.Add(num3 * xyz2).Subtract(basePoint).Normalize();
				XYZ endOrDirection = basePoint.Add(xyz3 * 100.0 / 304.8 * (double)doc.ActiveView.Scale / 100.0);
				subTransaction.Start();
				Line curve = doc.CreatYJKLine(basePoint, endOrDirection, true);
				DetailCurve detailCurve = doc.CreatYJKDetailCurve(curve);
				this.allDetailCurve.Insert(detailCurve);
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
		}

		private bool JudgePointQuadrant(XYZ point)
		{
			bool result = false;
			if ((point.X > 0.0 && point.Y < 0.0) || (point.X < 0.0 && point.Y > 0.0))
			{
				result = true;
			}
			return result;
		}

		private double GetWidthByRevitVersion(Autodesk.Revit.DB.Document doc, double width)
		{
			if (RevitVersion.Version == "2016" || RevitVersion.Version == "2017" || RevitVersion.Version == "2018")
			{
				width += (double)(15000 / doc.ActiveView.Scale);
			}
			else if (RevitVersion.Version == "2015" || RevitVersion.Version == "2014")
			{
				width *= 95.0;
			}
			return width;
		}

		private Pipe PickSinglePipe(ref XYZ pickPoint, bool bePlane, bool beHaveLine = false)
		{
			Pipe pipe = null;
			try
			{
				PipeDiameterMarkOperation.PipeTagParamter pipeTagParamter = null;
				Reference reference;
				if (bePlane)
				{
					reference = this.selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new HoriPipeFilter(), "请选择管道标注点……");
				}
				else
				{
					reference = this.selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new PipeFilter(), "请选择管道标注点……");
				}
				pipe = (this.doc.GetElementByRef(reference) as Pipe);
				if (this.UseForVrvLN && !this.IsCondensePipe(pipe))
				{
					YJKMessageBox.Show("该命令仅用于标注冷凝管!");
					return null;
				}
				pickPoint = reference.GlobalPoint;
				this.SetPipeDiameter(pipe);
				this.SetPipeParameter(pipe, pickPoint);
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				YJKLine hyLine = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				pipeTagParamter = this.LoadPipeTag(pipe);
				pipeTagParamter.PickPoint = YJKRevitTools.SetZToZero(hypiping.GetProject(pickPoint));
				pipeTagParamter.DynamicHVector = null;
				pipeTagParamter.DynamicVVector = null;
				double num = 0.0;
				if (beHaveLine && bePlane)
				{
					double num2 = this.GetPipeTagMaxLenght(new List<Pipe>
					{
						pipe
					}, ref num);
					num = this.GetWidthByRevitVersion(this.doc, num * 100.0 / (double)this.doc.ActiveView.Scale);
					num2 = this.GetWidthByRevitVersion(this.doc, num2 * 100.0 / (double)this.doc.ActiveView.Scale);
					MarkPipeLevelUtility.lineLenght = num2;
					using (CreatePipeLevelLinesJin createPipeLevelLinesJin = new CreatePipeLevelLinesJin(this.uiDoc, this.uiDoc.ActiveView, hyLine, true, false, true))
					{
						createPipeLevelLinesJin.StartPoint = pickPoint;
						this.dynamicPoint = this.selection.PickPoint("请选择标注方位点……");
						DirectionVector directionVector = createPipeLevelLinesJin.directionVector;
						this.dynamicVector = directionVector;
						List<Line> resultLines = createPipeLevelLinesJin.resultLines;
						this.DrawMuiltyLines(resultLines, this.doc);
						pipeTagParamter.PickPoint = this.dynamicPoint;
						pipeTagParamter.DynamicHVector = this.dynamicVector.HorizontalVector;
						pipeTagParamter.DynamicVVector = this.dynamicVector.VerticalVector;
					}
					this.userSetting = new UserSetting();
					PipeDiameterMarkSet diameterMarkSetting = this.userSetting.GetDiameterMarkSetting();
					if (this.CheckStringBeTrue(diameterMarkSetting.DrawSlash))
					{
						this.DrawSingleSlashLine(this.doc, pickPoint, pipeTagParamter.PickPoint, pipeTagParamter.DynamicHVector);
					}
				}
				if (beHaveLine && bePlane)
				{
					XYZ vVector = pipeTagParamter.DynamicVVector;
					if (pipeTagParamter.DynamicVVector.IsAlmostEqualTo(-1.0 * pipeTagParamter.PipeVVector, 0.0001))
					{
						vVector = -1.0 * pipeTagParamter.DynamicVVector;
					}
					this.CreatPipeIndependentTag(pipe, pipeTagParamter.PickPoint, vVector, pipeTagParamter.DynamicHVector, pipeTagParamter.FamilySymbol, num, false);
				}
				else if (!beHaveLine)
				{
					if (bePlane)
					{
						this.CreatPipeIndependentTag(pipe, pipeTagParamter.PickPoint, pipeTagParamter.PipeVVector, pipeTagParamter.PipeHVector, pipeTagParamter.FamilySymbol, true);
					}
					else
					{
						XYZ positiveVVector = this.GetPositiveVVector(pipe);
						XYZ positiveHVector = this.GetPositiveHVector(pipe);
						this.CreatPipeIndependentTag(pipe, pickPoint, positiveVVector, positiveHVector, pipeTagParamter.FamilySymbol, true);
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				if (!this.diameterMark.BeAutoCancel)
				{
					throw ex;
				}
				this.diameterMark.BeAutoCancel = false;
			}
			return pipe;
		}

		private IndependentTag CreatPipeIndependentTag(Pipe pipe, XYZ basePoint, XYZ vVector, XYZ hVector, FamilySymbol familySymbol, bool beToPipe = true)
		{
			IndependentTag independentTag = null;
			SubTransaction subTransaction = new SubTransaction(this.doc);
			try
			{
				if (YJKRevitTools.isRiserPipe(pipe))
				{
					hVector = new XYZ(0.0, 0.0, 1.0);
					vVector = new XYZ(1.0, 0.0, 0.0);
				}
				IndependentTag independentTag2 = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(pipe), false, 0, 0, new XYZ(0.0, 0.0, 0.0));
				independentTag2.ChangeTypeId(familySymbol.Id);
				MarkOperation markOperation = new MarkOperation();
				double num = 0.0;
				double num2 = 0.0;
				markOperation.GetTagTextWidthAndHeight(independentTag2.TagText, this.doc, ref num, ref num2);
				markOperation.GetCreatTagLength(independentTag2.TagText, this.doc);
				if (RevitVersion.GetVersionNumber(this.doc) == "2015" || RevitVersion.GetVersionNumber(this.doc) == "2014")
				{
					CommonFont fontSetting = new UserSetting().GetFontSetting();
					int length = independentTag2.TagText.Length;
					double fontGWRotia = fontSetting.FontGWRotia;
				}
				XYZ layPoint = this.GetLayPoint(pipe, basePoint, vVector, hVector, beToPipe, independentTag2);
				this.doc.DeleteElement(independentTag2);
				subTransaction.Start();
				PipeDiameterMarkOperation.PipeTagParamter pipeTagParamter = this.LoadPipeTag(pipe);
				independentTag = RevitVersionFuncs.NewTag(pipe.Document, this.doc.ActiveView, new Reference(pipe), false, 0, 0, layPoint);
				independentTag.ChangeTypeId(pipeTagParamter.FamilySymbol.Id);
				MarkOperation markOperation2 = new MarkOperation();
				double num3 = 0.0;
				double num4 = 0.0;
				markOperation2.GetTagTextWidthAndHeight(independentTag.TagText, this.doc, ref num3, ref num4);
				num3 = this.GetWidthByRevitVersion(this.doc, num3);
				double num5 = num3 / 2.0 / 304.8;
				AssociationMarkInfo.WriteMarkInfoToElement(independentTag, MarkType.PipeDiameter);
				new YJKRotateFamilyInstance().MoveElement(num5 * hVector, independentTag);
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				ex.ToString();
				if (subTransaction != null && (int)subTransaction.GetStatus() == 1)
				{
					subTransaction.RollBack();
				}
			}
			return independentTag;
		}

		private IndependentTag CreatPipeIndependentTag(Pipe pipe, XYZ basePoint, XYZ vVector, XYZ hVector, FamilySymbol familySymbol, double tagWidth, bool beToPipe = true)
		{
			IndependentTag independentTag = null;
			SubTransaction subTransaction = new SubTransaction(this.doc);
			try
			{
				if (YJKRevitTools.isRiserPipe(pipe))
				{
					hVector = new XYZ(0.0, 0.0, 1.0);
					vVector = new XYZ(1.0, 0.0, 0.0);
				}
				IndependentTag independentTag2 = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(pipe), false, 0, 0, new XYZ(0.0, 0.0, 0.0));
				independentTag2.ChangeTypeId(familySymbol.Id);
				MarkOperation markOperation = new MarkOperation();
				double num = 0.0;
				double num2 = 0.0;
				markOperation.GetTagTextWidthAndHeight(independentTag2.TagText, this.doc, ref num, ref num2);
				markOperation.GetCreatTagLength(independentTag2.TagText, this.doc);
				if (RevitVersion.GetVersionNumber(this.doc) == "2015" || RevitVersion.GetVersionNumber(this.doc) == "2014")
				{
					CommonFont fontSetting = new UserSetting().GetFontSetting();
					int length = independentTag2.TagText.Length;
					double fontGWRotia = fontSetting.FontGWRotia;
				}
				XYZ layPoint = this.GetLayPoint(pipe, basePoint, vVector, hVector, beToPipe, independentTag2);
				this.doc.DeleteElement(independentTag2);
				subTransaction.Start();
				independentTag = RevitVersionFuncs.NewTag(pipe.Document, this.doc.ActiveView, new Reference(pipe), false, 0, 0, layPoint);
				independentTag.ChangeTypeId(familySymbol.Id);
				double creatTagLength = this.GetCreatTagLength(independentTag.TagText, pipe.Document);
				double num3 = this.GetWidthByRevitVersion(this.doc, creatTagLength) / 2.0 / 304.8;
				AssociationMarkInfo.WriteMarkInfoToElement(independentTag, MarkType.PipeDiameter);
				new YJKRotateFamilyInstance().MoveElement(num3 * hVector, independentTag);
				subTransaction.Commit();
				if (MarkPipeLevelUtility.leaderLines.Count > 0)
				{
					new MarkOperation().SetAlignStyle(independentTag, new YJKLine(MarkPipeLevelUtility.leaderLines.First<Line>()), basePoint, "水管");
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				if (subTransaction != null && (int)subTransaction.GetStatus() == 1)
				{
					subTransaction.RollBack();
				}
			}
			return independentTag;
		}

		private XYZ OffsetPointByAlign(XYZ layPoint, double tagLength)
		{
			XYZ result = XYZ.Zero;
			double val = Math.Abs((MarkPipeLevelUtility.lineLenght - tagLength) / 2.0) * this.commonFont.LeadRotia;
			string align = this.commonFont.Align;
			if (!(align == "始端对齐"))
			{
				if (!(align == "末端对齐"))
				{
					if (!(align == "居中对齐"))
					{
					}
					result = layPoint;
				}
				else
				{
					result = layPoint + 0.95 * this.dynamicVector.HorizontalVector * AssistFunc.mmToFeet(val) * (double)this.doc.ActiveView.Scale / 100.0;
				}
			}
			else
			{
				result = layPoint - 0.95 * this.dynamicVector.HorizontalVector * AssistFunc.mmToFeet(val) * (double)this.doc.ActiveView.Scale / 100.0;
			}
			return result;
		}

		private IndependentTag CreatPipeIndependentTag(Pipe pipe, XYZ basePoint, XYZ vVector, XYZ hVector, FamilySymbol familySymbol, XYZ moveVector)
		{
			IndependentTag independentTag = null;
			SubTransaction subTransaction = new SubTransaction(this.doc);
			try
			{
				subTransaction.Start();
				XYZ layPoint = this.GetLayPoint(pipe, basePoint, vVector, hVector, true);
				PipeDiameterMarkOperation.PipeTagParamter pipeTagParamter = this.LoadPipeTag(pipe);
				independentTag = RevitVersionFuncs.NewTag(pipe.Document, this.doc.ActiveView, new Reference(pipe), false, 0, 0, layPoint);
				independentTag.ChangeTypeId(pipeTagParamter.FamilySymbol.Id);
				double num = this.GetTagTextLenght(independentTag.TagText) / 2.0 / 304.8;
				XYZ positiveHVector = this.GetPositiveHVector(pipe);
				AssociationMarkInfo.WriteMarkInfoToElement(independentTag, MarkType.PipeDiameter);
				if (positiveHVector.IsAlmostEqualTo(moveVector * -1.0, 0.0001))
				{
					num = -1.0 * num;
				}
				new YJKRotateFamilyInstance().MoveElement(num * hVector, independentTag);
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				ex.ToString();
				if (subTransaction != null && (int)subTransaction.GetStatus() == 1)
				{
					subTransaction.RollBack();
				}
			}
			return independentTag;
		}

		private XYZ GetLayPoint(Pipe pipe, XYZ basePoint, XYZ vVector, XYZ hVector, bool beToPipe)
		{
			XYZ result = null;
			this.userSetting = new UserSetting();
			this.userSetting.GetFontSetting();
			double num;
			if (beToPipe)
			{
				num = this.GetDistanceTagToPipe(pipe);
				if (this.diameterMark.GetMarkWay() == DiameterMarkWay.SingleDown || this.diameterMark.GetMarkWay() == DiameterMarkWay.MuiltyDown)
				{
					vVector = -1.0 * vVector;
				}
			}
			else
			{
				double num2 = this.GetCreatTagHeight(pipe) / 304.8;
				new MarkOperation();
				num = this.GetDistanceTagToLeader(this.doc, num2 * 0.8);
			}
			if (RevitVersion.Version == "2015" || RevitVersion.Version == "2016" || RevitVersion.Version == "2017" || RevitVersion.Version == "2018")
			{
				result = basePoint.Add(num * vVector.Normalize());
			}
			else if (RevitVersion.Version == "2019")
			{
				result = basePoint.Add(num * vVector.Normalize());
			}
			return result;
		}

		private XYZ GetLayPoint(Pipe pipe, XYZ basePoint, XYZ vVector, XYZ hVector, bool beToPipe, IndependentTag tag)
		{
			this.userSetting = new UserSetting();
			this.userSetting.GetFontSetting();
			double num;
			if (beToPipe)
			{
				num = this.GetDistanceTagToPipe(pipe);
				if (this.diameterMark.GetMarkWay() == DiameterMarkWay.SingleDown || this.diameterMark.GetMarkWay() == DiameterMarkWay.MuiltyDown)
				{
					vVector = -1.0 * vVector;
				}
			}
			else
			{
				double num2 = this.GetCreatTagHeight(pipe) / 304.8;
				num = new MarkOperation().GetDistanceTagToLeader(this.doc, tag.TagText);
			}
			return basePoint.Add(num * vVector.Normalize());
		}

		private List<Pipe> PickRectanglePipes(bool bePlane)
		{
			List<Pipe> list = null;
			try
			{
				IEnumerable<Element> enumerable = this.selection.PickElementsByRectangle(new HoriPipeFilter(), "请框选多根管道……");
				list = new List<Pipe>();
				foreach (Element element in enumerable)
				{
					if (element is Pipe && (!this.UseForVrvLN || this.IsCondensePipe(element as Pipe)))
					{
						list.Add(element as Pipe);
					}
				}
				if (list == null || list.Count < 1)
				{
					return null;
				}
				this.SetPipeDiameter(list);
				this.CreatPipesTag(list, bePlane);
			}
			catch (Exception ex)
			{
				ex.ToString();
				if (!this.diameterMark.BeAutoCancel)
				{
					throw ex;
				}
				this.diameterMark.BeAutoCancel = false;
			}
			return list;
		}

		private void SetPipeDiameter(List<Pipe> listPipe)
		{
			if (!this.diameterMark.BeSettingDiamter())
			{
				return;
			}
			double settingDiameter = this.diameterMark.GetSettingDiameter();
			foreach (Pipe elem in listPipe)
			{
				elem.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(settingDiameter);
			}
		}

		private void SetPipeDiameter(Pipe pipe)
		{
			if (!this.diameterMark.BeSettingDiamter())
			{
				return;
			}
			double settingDiameter = this.diameterMark.GetSettingDiameter();
			pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(settingDiameter);
			pipe.Document.Regenerate();
		}

		private void CreatPipesTag(List<Pipe> listPipe, bool bePlane)
		{
			PipeDiameterMarkOperation.PipeTagParamter pipeTagParamter = this.LoadPipeTag(listPipe.First<Pipe>());
			this.userSetting = new UserSetting();
			PipeDiameterMarkSet diaMarkSet = this.userSetting.GetDiameterMarkSetting();
			listPipe = (from pipe in listPipe
			where (pipe.Location as LocationCurve).Curve.Length * 304.8 > diaMarkSet.TubeLength && new YJKPiping<Pipe>(pipe).GetDiameter() * 304.8 > diaMarkSet.TubeDiameter
			select pipe).ToList<Pipe>();
			GroupingPipes groupingPipes = new GroupingPipes(listPipe);
			groupingPipes.SpliteAndGroupPipes();
			foreach (List<Pipe> listPipe2 in groupingPipes.GroupedPipes)
			{
				this.MarkMuiltyPipes(listPipe2, pipeTagParamter.FamilySymbol, diaMarkSet);
			}
		}

		private void MarkMuiltyPipes(List<Pipe> listPipe, FamilySymbol fSymbol, PipeDiameterMarkSet diaMarkSet)
		{
			if (listPipe == null || listPipe.Count < 1)
			{
				return;
			}
			listPipe = (from p in listPipe
			orderby this.GetPipeLength(p)
			select p).ToList<Pipe>();
			XYZ pipeMiddlePoint = this.GetPipeMiddlePoint(listPipe.First<Pipe>());
			foreach (Pipe pipe in listPipe)
			{
				Dictionary<XYZ, XYZ> markTagPoints = this.GetMarkTagPoints(pipe, diaMarkSet, pipeMiddlePoint);
				this.CreatMarkTagSinglePipe(pipe, fSymbol, markTagPoints);
			}
		}

		private void CreatMarkTagSinglePipe(Pipe pipe, FamilySymbol fSymbol, Dictionary<XYZ, XYZ> layPoints)
		{
			XYZ positiveVVector = this.GetPositiveVVector(pipe);
			XYZ positiveHVector = this.GetPositiveHVector(pipe);
			if (layPoints == null || layPoints.Count < 1)
			{
				return;
			}
			foreach (KeyValuePair<XYZ, XYZ> keyValuePair in layPoints)
			{
				this.SetPipeParameter(pipe, keyValuePair.Key);
				if (keyValuePair.Value == null)
				{
					this.CreatPipeIndependentTag(pipe, keyValuePair.Key, positiveVVector, positiveHVector, fSymbol, true);
				}
				else
				{
					this.CreatPipeIndependentTag(pipe, keyValuePair.Key, positiveVVector, positiveHVector, fSymbol, keyValuePair.Value);
				}
			}
		}

		private Dictionary<XYZ, XYZ> GetMarkTagPoints(Pipe pipe, PipeDiameterMarkSet diaMarkSet, XYZ basePoint)
		{
			Dictionary<XYZ, XYZ> dictionary = new Dictionary<XYZ, XYZ>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			double pipeLength = this.GetPipeLength(pipe);
			double num = hypiping.GetDiameter() * 304.8;
			if (diaMarkSet.TubeLength > pipeLength || diaMarkSet.TubeDiameter > num)
			{
				return null;
			}
			XYZ key;
			if (basePoint == null)
			{
				key = this.GetPipeMiddlePoint(pipe);
			}
			else
			{
				key = hypiping.GetProject(basePoint);
			}
			dictionary.Add(key, null);
			XYZ positiveHVector = this.GetPositiveHVector(pipe);
			this.GetPositiveVVector(pipe);
			double num2 = hypiping.GetDiameter() * 3.0;
			if (this.CheckStringBeTrue(diaMarkSet.TwoSides))
			{
				List<XYZ> pipeIntersetWallPoint = this.GetPipeIntersetWallPoint(pipe);
				if (pipeIntersetWallPoint != null && pipeIntersetWallPoint.Count > 0)
				{
					foreach (XYZ point in pipeIntersetWallPoint)
					{
						XYZ project = hypiping.GetProject(point);
						XYZ xyz = project.Add(positiveHVector * num2);
						bool flag = this.CheckPointsDistance(dictionary, xyz, diaMarkSet.TubeLength, hypiping);
						XYZ xyz2 = project.Add(-1.0 * positiveHVector * num2);
						bool flag2 = this.CheckPointsDistance(dictionary, xyz2, diaMarkSet.TubeLength, hypiping);
						if (flag)
						{
							dictionary.Add(xyz, positiveHVector);
						}
						if (flag2)
						{
							dictionary.Add(xyz2, positiveHVector * -1.0);
						}
					}
				}
			}
			if (this.CheckStringBeTrue(diaMarkSet.Reduce))
			{
				List<Connector> reduceConn = this.GetReduceConn(pipe);
				if (reduceConn != null && reduceConn.Count > 0)
				{
					foreach (Connector connector in reduceConn)
					{
						XYZ origin = connector.Origin;
						XYZ xyz3 = connector.CoordinateSystem.BasisZ * -1.0;
						XYZ xyz4 = origin.Add(xyz3 * num2);
						if (this.CheckPointsDistance(dictionary, xyz4, diaMarkSet.TubeLength, hypiping))
						{
							dictionary.Add(xyz4, xyz3);
						}
					}
				}
			}
			return dictionary;
		}

		private bool CheckPointsDistance(Dictionary<XYZ, XYZ> markPoints, XYZ point, double minDistance, YJKPiping<Pipe> hyPipe)
		{
			bool result = true;
			if (hyPipe.IsPointOnPipingCurveExtension(point))
			{
				return false;
			}
			foreach (KeyValuePair<XYZ, XYZ> keyValuePair in markPoints)
			{
				if (keyValuePair.Key.DistanceTo(point) * 304.8 < minDistance)
				{
					result = false;
					break;
				}
			}
			return result;
		}

		private void CreatWallTowSidesPipe(List<Pipe> listPipe, FamilySymbol familySymbol)
		{
			foreach (Pipe pipe in listPipe)
			{
				List<XYZ> pipeIntersetWallPoint = this.GetPipeIntersetWallPoint(pipe);
				if (pipeIntersetWallPoint != null && pipeIntersetWallPoint.Count >= 1)
				{
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
					XYZ positiveHVector = this.GetPositiveHVector(pipe);
					foreach (XYZ point in pipeIntersetWallPoint)
					{
						XYZ project = hypiping.GetProject(point);
						this.CreatSideTag(pipe, project, familySymbol, positiveHVector);
						this.CreatSideTag(pipe, project, familySymbol, positiveHVector * -1.0);
					}
				}
			}
		}

		private XYZ GetPipeMiddlePoint(Pipe pipe)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			return origin.Add(origin2) / 2.0;
		}

		private double GetPipeLength(Pipe pipe)
		{
			return new YJKPiping<Pipe>(pipe).GetLocationCurve().ApproximateLength * 304.8;
		}

		private void CreatReducePipe(List<Pipe> listPipe, FamilySymbol familySymbol)
		{
			foreach (Pipe pipe in listPipe)
			{
				List<Connector> reduceConn = this.GetReduceConn(pipe);
				if (reduceConn != null && reduceConn.Count >= 1)
				{
					new YJKPiping<Pipe>(pipe);
					this.GetPositiveHVector(pipe);
					foreach (Connector connector in reduceConn)
					{
						XYZ origin = connector.Origin;
						this.CreatSideTag(pipe, origin, familySymbol, connector.CoordinateSystem.BasisZ * -1.0);
					}
				}
			}
		}

		private void CreatSideTag(Pipe pipe, XYZ onPipePoint, FamilySymbol familySymbol, XYZ moveVector)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ positiveHVector = this.GetPositiveHVector(pipe);
			XYZ positiveVVector = this.GetPositiveVVector(pipe);
			double num = hypiping.GetDiameter() * 3.0;
			XYZ xyz = onPipePoint.Add(moveVector * num);
			if (hypiping.IsPointOnPipingCurveExtension(xyz))
			{
				return;
			}
			this.CreatPipeIndependentTag(pipe, xyz, positiveVVector, positiveHVector, familySymbol, moveVector);
		}

		private List<Pipe> SelectPipes(ref XYZ markLocationPoint)
		{
			List<Pipe> list = null;
			try
			{
				XYZ markPoint = null;
				XYZ vVector = null;
				string value = "";
				do
				{
					list = this.GetPipesByTwoPoints(ref markPoint, ref vVector, ref value);
					if ((list == null || list.Count < 1) && string.IsNullOrEmpty(value))
					{
						YJKMessageBox.Information("没有选中任何管道，请重新选择……");
					}
					else if (!string.IsNullOrEmpty(value))
					{
						break;
					}
				}
				while (list == null || list.Count < 1);
				if (list != null && list.Count > 0)
				{
					this.SetPipeDiameter(list);
					this.MuiltyDynamicShow(list, markPoint, vVector);
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				if (!this.diameterMark.BeAutoCancel)
				{
					throw ex;
				}
				this.diameterMark.BeAutoCancel = false;
			}
			return list;
		}

		private PipeDiameterMarkOperation.PipeTagParamter LoadPipeTag(Pipe pipe)
		{
			PipeDiameterMarkOperation.PipeTagParamter pipeTagParamter = new PipeDiameterMarkOperation.PipeTagParamter();
			string familyName = "";
			string text = "";
			this.GetTagNameNew(ref familyName, ref text, pipe);
			pipeTagParamter.BasePipe = pipe;
			pipeTagParamter.FamilyName = familyName;
			pipeTagParamter.SymbolName = text;
			pipeTagParamter.PipeHVector = this.GetPositiveHVector(pipe);
			pipeTagParamter.PipeVVector = this.GetPositiveVVector(pipe);
			string text2 = "";
			pipeTagParamter.FamilySymbol = YJKRevitTools.GetMarkTagSymbolByName("MarkTag", familyName, text, this.doc, ref text2, BuiltInCategory.OST_PipeTags);
			return pipeTagParamter;
		}

		private void SetDynamicDefaultValue()
		{
			this.userSetting = new UserSetting();
			CommonFont fontSetting = this.userSetting.GetFontSetting();
			double fontHigtht = 1.0;
			double fontWidth = 1.0;
			if (fontSetting.FontHeight > 0.0 && fontSetting.FontGWRotia > 0.0)
			{
				fontHigtht = fontSetting.FontHeight / 3.0;
				fontWidth = fontSetting.FontGWRotia * 1.4285714285714286;
			}
			MarkPipeLevelUtility.fontHigtht = fontHigtht;
			MarkPipeLevelUtility.fontWidth = fontWidth;
			MarkPipeLevelUtility.leadDistance = fontSetting.LeadDistance;
		}

		private string GetTagNameNew(ref string familyName, ref string symbolName, Pipe pipe)
		{
			this.userSetting = new UserSetting();
			CommonFont fontSetting = this.userSetting.GetFontSetting();
			this.markSet = this.userSetting.GetDiameterMarkSetting();
			PipeOffsetMarkSet offsetMarkSetting = this.userSetting.GetOffsetMarkSetting();
			string pipePrefix = this.GetPipePrefix(pipe);
			PipeDiameterMarkOperation.MarkDiameterType prefixType = this.GetPrefixType(pipePrefix);
			if (this.CheckStringBeTrue(this.markSet.SystemType) && this.CheckStringBeTrue(this.markSet.PipeOffset))
			{
				PipeOffsetMarkSet offsetMarkSetting2 = this.userSetting.GetOffsetMarkSetting();
				this.JudgeBeShow(offsetMarkSetting2.UnitVisible);
				if (offsetMarkSetting2.Content.Contains("绝对标高"))
				{
					if (this.BeMeterToUnit(offsetMarkSetting.Suffix))
					{
						if (fontSetting.Presice.Length == 4)
						{
							familyName = "HY_水管系统管径绝对标高标注_两位小数";
							if (offsetMarkSetting2.Splite.Equals("空格"))
							{
								familyName = "HY_水管系统管径绝对标高标注_空格_两位小数";
							}
						}
						else
						{
							familyName = "HY_水管系统管径绝对标高标注_三位小数";
							if (offsetMarkSetting2.Splite.Equals("空格"))
							{
								familyName = "HY_水管系统管径绝对标高标注_空格_三位小数";
							}
						}
					}
					else
					{
						familyName = "HY_水管系统管径绝对标高标注_毫米";
						if (offsetMarkSetting2.Splite.Equals("空格"))
						{
							familyName = "HY_水管系统管径绝对标高标注_空格_毫米";
						}
					}
					symbolName = this.GetTagSymbolName(prefixType, PipeDiameterMarkOperation.MarkSymbol.SystemAbsolutely);
				}
				else
				{
					if (this.BeMeterToUnit(offsetMarkSetting.Suffix))
					{
						if (fontSetting.Presice.Length == 4)
						{
							familyName = "HY_水管系统管径标高标注_两位小数";
							if (offsetMarkSetting2.Splite.Equals("空格"))
							{
								familyName = "HY_水管系统管径标高标注_空格_两位小数";
							}
						}
						else
						{
							familyName = "HY_水管系统管径标高标注_三位小数";
							if (offsetMarkSetting2.Splite.Equals("空格"))
							{
								familyName = "HY_水管系统管径标高标注_空格_三位小数";
							}
						}
					}
					else
					{
						familyName = "HY_水管系统管径标高标注_毫米";
						if (offsetMarkSetting2.Splite.Equals("空格"))
						{
							familyName = "HY_水管系统管径标高标注_空格_毫米";
						}
					}
					symbolName = this.GetTagSymbolName(prefixType, PipeDiameterMarkOperation.MarkSymbol.SystemRelative);
				}
			}
			else if (this.CheckStringBeTrue(this.markSet.PipeOffset))
			{
				PipeOffsetMarkSet offsetMarkSetting3 = this.userSetting.GetOffsetMarkSetting();
				if (offsetMarkSetting3.Content.Contains("绝对标高"))
				{
					if (this.BeMeterToUnit(offsetMarkSetting.Suffix))
					{
						if (fontSetting.Presice.Length == 4)
						{
							familyName = "HY_水管管径绝对标高标注_两位小数";
							if (offsetMarkSetting3.Splite.Equals("空格"))
							{
								familyName = "HY_水管管径绝对标高标注_空格_两位小数";
							}
						}
						else
						{
							familyName = "HY_水管管径绝对标高标注_三位小数";
							if (offsetMarkSetting3.Splite.Equals("空格"))
							{
								familyName = "HY_水管管径绝对标高标注_空格_三位小数";
							}
						}
					}
					else
					{
						familyName = "HY_水管管径绝对标高标注_毫米";
						if (offsetMarkSetting3.Splite.Equals("空格"))
						{
							familyName = "HY_水管管径绝对标高标注_空格_毫米";
						}
					}
					symbolName = this.GetTagSymbolName(prefixType, PipeDiameterMarkOperation.MarkSymbol.Absolutely);
				}
				else
				{
					if (this.BeMeterToUnit(offsetMarkSetting.Suffix))
					{
						if (fontSetting.Presice.Length == 4)
						{
							familyName = "HY_水管管径标高标注_两位小数";
							if (offsetMarkSetting3.Splite.Equals("空格"))
							{
								familyName = "HY_水管管径标高标注_空格_两位小数";
							}
						}
						else
						{
							familyName = "HY_水管管径标高标注_三位小数";
							if (offsetMarkSetting3.Splite.Equals("空格"))
							{
								familyName = "HY_水管管径标高标注_空格_三位小数";
							}
						}
					}
					else
					{
						familyName = "HY_水管管径标高标注_毫米";
						if (offsetMarkSetting3.Splite.Equals("空格"))
						{
							familyName = "HY_水管管径标高标注_空格_毫米";
						}
					}
					symbolName = this.GetTagSymbolName(prefixType, PipeDiameterMarkOperation.MarkSymbol.Relative);
				}
			}
			else if (this.CheckStringBeTrue(this.markSet.SystemType))
			{
				familyName = "HY_水管系统管径标注";
				symbolName = this.GetTagSymbolName(prefixType, PipeDiameterMarkOperation.MarkSymbol.System);
			}
			else if (this.CheckStringBeTrue(this.markSet.Thickness))
			{
				familyName = "HY_水管管径壁厚标注";
				if (this.markSet.Splite.Equals("空格"))
				{
					familyName = "HY_水管管径壁厚标注_空格";
				}
				symbolName = this.GetTagSymbolName(prefixType, PipeDiameterMarkOperation.MarkSymbol.Thickness);
			}
			else
			{
				familyName = "HY_水管管径标注";
				symbolName = this.GetTagSymbolName(prefixType, PipeDiameterMarkOperation.MarkSymbol.Diameter);
			}
			return this.GetTagText(pipe, familyName, symbolName);
		}

		private bool BeMeterToUnit(string suffixValue)
		{
			return suffixValue.Equals("m");
		}

		private string GetTagText(Pipe pipe, string familyName, string symbolName)
		{
			string result = "";
			string text = "";
			FamilySymbol markTagSymbolByName = YJKRevitTools.GetMarkTagSymbolByName("MarkTag", familyName, symbolName, this.doc, ref text, BuiltInCategory.OST_PipeTags);
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ xyz = hypiping.StartConnector.Origin.Add(hypiping.EndConnector.Origin) / 2.0;
			SubTransaction subTransaction = new SubTransaction(this.doc);
			try
			{
				subTransaction.Start();
				XYZ pnt = xyz;
				IndependentTag independentTag = RevitVersionFuncs.NewTag(pipe.Document, this.doc.ActiveView, new Reference(pipe), false, 0, 0, pnt);
				independentTag.ChangeTypeId(markTagSymbolByName.Id);
				pipe.Document.Regenerate();
				result = independentTag.TagText;
				subTransaction.RollBack();
			}
			catch (Exception ex)
			{
				ex.ToString();
				if (subTransaction != null && (int)subTransaction.GetStatus() == 1)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private string AddSpaceStr(int spaceNum, string textTag)
		{
			if (spaceNum > 0)
			{
				for (int i = 0; i < spaceNum; i++)
				{
					textTag += " ";
				}
			}
			return textTag;
		}

		private string GetSystemAbbreviation(Pipe pipe)
		{
			return pipe.GetParameter(BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM).AsString();
		}

		private string GetTagSymbolName(PipeDiameterMarkOperation.MarkDiameterType markType, PipeDiameterMarkOperation.MarkSymbol markSymbol)
		{
			string result = "";
			switch (markSymbol)
			{
			case PipeDiameterMarkOperation.MarkSymbol.SystemAbsolutely:
				switch (markType)
				{
				case PipeDiameterMarkOperation.MarkDiameterType.Diameter:
					result = "系统直径绝对标高";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.OutDiameter:
					result = "系统外径绝对标高";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter:
					result = "系统内径绝对标高";
					break;
				}
				break;
			case PipeDiameterMarkOperation.MarkSymbol.SystemRelative:
				switch (markType)
				{
				case PipeDiameterMarkOperation.MarkDiameterType.Diameter:
					result = "系统直径标高";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.OutDiameter:
					result = "系统外径标高";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter:
					result = "系统内径标高";
					break;
				}
				break;
			case PipeDiameterMarkOperation.MarkSymbol.Absolutely:
				switch (markType)
				{
				case PipeDiameterMarkOperation.MarkDiameterType.Diameter:
					result = "直径绝对标高";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.OutDiameter:
					result = "外径绝对标高";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter:
					result = "内径绝对标高";
					break;
				}
				break;
			case PipeDiameterMarkOperation.MarkSymbol.Relative:
				switch (markType)
				{
				case PipeDiameterMarkOperation.MarkDiameterType.Diameter:
					result = "系统直径标高";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.OutDiameter:
					result = "系统外径标高";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter:
					result = "系统内径标高";
					break;
				}
				break;
			case PipeDiameterMarkOperation.MarkSymbol.System:
			case PipeDiameterMarkOperation.MarkSymbol.Diameter:
				switch (markType)
				{
				case PipeDiameterMarkOperation.MarkDiameterType.Diameter:
					result = "水管直径";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.OutDiameter:
					result = "水管外径";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter:
					result = "水管内径";
					break;
				}
				break;
			case PipeDiameterMarkOperation.MarkSymbol.Thickness:
				switch (markType)
				{
				case PipeDiameterMarkOperation.MarkDiameterType.Diameter:
					result = "直径壁厚";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.OutDiameter:
					result = "外径壁厚";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter:
					result = "内径壁厚";
					break;
				}
				break;
			}
			return result;
		}

		private string GetTagName(ref string familyName, ref string symbolName, Pipe pipe)
		{
			string text = "";
			string pipePrefix = this.GetPipePrefix(pipe);
			PipeDiameterMarkOperation.MarkDiameterType prefixType = this.GetPrefixType(pipePrefix);
			text += pipePrefix;
			text += this.GetDiamterString(pipe, prefixType);
			this.userSetting = new UserSetting();
			this.markSet = this.userSetting.GetDiameterMarkSetting();
			if (this.CheckStringBeTrue(this.markSet.SystemType) && this.CheckStringBeTrue(this.markSet.PipeOffset))
			{
				Parameter parameter = pipe.GetParameter(BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM);
				text = text + this.GetParameterValue(parameter) + "  ";
				PipeOffsetMarkSet offsetMarkSetting = this.userSetting.GetOffsetMarkSetting();
				text += this.ConnAndFixString(offsetMarkSetting.Splite);
				text += this.ConnAndFixString(offsetMarkSetting.Suffix);
				if (offsetMarkSetting.Content.Contains("绝对标高"))
				{
					text += this.GetAbsoluteLevel(pipe);
					familyName = "HY_水管系统管径绝对标高标注";
					if (offsetMarkSetting.Splite.Equals("空格"))
					{
						familyName = "HY_水管系统管径绝对标高标注_空格";
					}
					switch (prefixType)
					{
					case PipeDiameterMarkOperation.MarkDiameterType.Diameter:
						symbolName = "系统直径绝对标高";
						break;
					case PipeDiameterMarkOperation.MarkDiameterType.OutDiameter:
						symbolName = "系统外径绝对标高";
						break;
					case PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter:
						symbolName = "系统内径绝对标高";
						break;
					}
				}
				else
				{
					text += this.ConnAndFixString(offsetMarkSetting.Prefix);
					text += this.GetOffset(pipe).ToString();
					familyName = "HY_水管系统管径标高标注";
					if (offsetMarkSetting.Splite.Equals("空格"))
					{
						familyName = "HY_水管系统管径标高标注_空格";
					}
					switch (prefixType)
					{
					case PipeDiameterMarkOperation.MarkDiameterType.Diameter:
						symbolName = "系统直径标高";
						break;
					case PipeDiameterMarkOperation.MarkDiameterType.OutDiameter:
						symbolName = "系统外径标高";
						break;
					case PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter:
						symbolName = "系统内径标高";
						break;
					}
				}
			}
			else if (this.CheckStringBeTrue(this.markSet.PipeOffset))
			{
				PipeOffsetMarkSet offsetMarkSetting2 = this.userSetting.GetOffsetMarkSetting();
				text = text + this.ConnAndFixString(offsetMarkSetting2.Splite) + " ";
				text += this.ConnAndFixString(offsetMarkSetting2.Suffix);
				if (offsetMarkSetting2.Content.Contains("绝对标高"))
				{
					text += this.GetAbsoluteLevel(pipe);
					familyName = "HY_水管管径绝对标高标注";
					if (offsetMarkSetting2.Splite.Equals("空格"))
					{
						familyName = "HY_水管管径绝对标高标注_空格";
					}
					switch (prefixType)
					{
					case PipeDiameterMarkOperation.MarkDiameterType.Diameter:
						symbolName = "直径绝对标高";
						break;
					case PipeDiameterMarkOperation.MarkDiameterType.OutDiameter:
						symbolName = "外径绝对标高";
						break;
					case PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter:
						symbolName = "内径绝对标高";
						break;
					}
				}
				else
				{
					text += this.ConnAndFixString(offsetMarkSetting2.Prefix);
					text += this.GetOffset(pipe).ToString();
					familyName = "HY_水管管径标高标注";
					if (offsetMarkSetting2.Splite.Equals("空格"))
					{
						familyName = "HY_水管管径标高标注_空格";
					}
					switch (prefixType)
					{
					case PipeDiameterMarkOperation.MarkDiameterType.Diameter:
						symbolName = "系统直径标高";
						break;
					case PipeDiameterMarkOperation.MarkDiameterType.OutDiameter:
						symbolName = "系统外径标高";
						break;
					case PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter:
						symbolName = "系统内径标高";
						break;
					}
				}
			}
			else if (this.CheckStringBeTrue(this.markSet.SystemType))
			{
				Parameter parameter = pipe.GetParameter(BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM);
				text = text + this.GetParameterValue(parameter) + " ";
				familyName = "HY_水管系统管径标注";
				switch (prefixType)
				{
				case PipeDiameterMarkOperation.MarkDiameterType.Diameter:
					symbolName = "水管直径";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.OutDiameter:
					symbolName = "水管外径";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter:
					symbolName = "水管内径";
					break;
				}
			}
			else if (this.CheckStringBeTrue(this.markSet.Thickness))
			{
				text = text + this.GetPipeThickness(pipe) + " ";
				familyName = "HY_水管管径壁厚标注";
				if (this.markSet.Splite.Equals("空格"))
				{
					familyName = "HY_水管管径壁厚标注_空格";
				}
				switch (prefixType)
				{
				case PipeDiameterMarkOperation.MarkDiameterType.Diameter:
					symbolName = "直径壁厚";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.OutDiameter:
					symbolName = "外径壁厚";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter:
					symbolName = "内径壁厚";
					break;
				}
			}
			else
			{
				familyName = "HY_水管管径标注";
				switch (prefixType)
				{
				case PipeDiameterMarkOperation.MarkDiameterType.Diameter:
					symbolName = "水管直径";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.OutDiameter:
					symbolName = "水管外径";
					break;
				case PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter:
					symbolName = "水管内径";
					break;
				}
			}
			return text;
		}

		private string ConnAndFixString(string strValue)
		{
			string result = strValue;
			if (!(strValue == "空格"))
			{
				if (strValue == "无")
				{
					result = "";
				}
			}
			else
			{
				result = " ";
			}
			return result;
		}

		private string GetPipeThickness(Pipe pipe)
		{
			return pipe.GetParameter("壁厚").AsValueString();
		}

		private string GetDiamterString(Pipe pipe, PipeDiameterMarkOperation.MarkDiameterType markType)
		{
			string result = "";
			switch (markType)
			{
			case PipeDiameterMarkOperation.MarkDiameterType.Diameter:
				result = this.GetDiameter(pipe, true);
				break;
			case PipeDiameterMarkOperation.MarkDiameterType.OutDiameter:
				result = this.GetOutDiameter(pipe, true);
				break;
			case PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter:
				result = this.GetInnerDiameter(pipe, true);
				break;
			}
			return result;
		}

		private PipeDiameterMarkOperation.MarkDiameterType GetPrefixType(string prefixValue)
		{
			PipeDiameterMarkOperation.MarkDiameterType result = PipeDiameterMarkOperation.MarkDiameterType.Diameter;
			if (prefixValue.Equals("De") || prefixValue.Equals("D") || prefixValue.Equals("dn"))
			{
				result = PipeDiameterMarkOperation.MarkDiameterType.OutDiameter;
			}
			else if (prefixValue.Equals("d"))
			{
				result = PipeDiameterMarkOperation.MarkDiameterType.InnerDiameter;
			}
			return result;
		}

		private string GetPipePrefix(Pipe pipe)
		{
			this.userSetting = new UserSetting();
			string result = "DN";
			string pipeMetiral = this.GetPipeMetiral(pipe);
			this.allSetting = this.userSetting.GetAllPrefixSetting();
			foreach (PrefixSetting prefixSetting in this.allSetting)
			{
				if (pipeMetiral.Contains(prefixSetting.MaterialName))
				{
					result = prefixSetting.PrefixValue;
					break;
				}
			}
			return result;
		}

		private string GetPipeMetiral(Pipe pipe)
		{
			Document document = pipe.Document;
			return pipe.PipeType.Name;
		}

		private bool CheckStringBeTrue(string strValue)
		{
			return strValue.ToLower().Equals("true");
		}

		private string GetParameterValue(Parameter param)
		{
			string result = "";
			if (param == null || !param.HasValue)
			{
				return result;
			}
            switch ((int)param.StorageType)
			{
			case 1:
				result = param.AsInteger().ToString();
				break;
			case 2:
				result = param.AsDouble().ToString();
				break;
			case 3:
				result = param.AsString();
				break;
			}
			return result;
		}

		private double GetOutDiameter(Pipe pipe)
		{
			return pipe.GetParameter(BuiltInParameter.RBS_PIPE_OUTER_DIAMETER).AsDouble() * 304.8;
		}

		private string GetOutDiameter(Pipe pipe, bool beString)
		{
			return pipe.GetParameter(BuiltInParameter.RBS_PIPE_OUTER_DIAMETER).AsValueString();
		}

		private double GetInnerDiameter(Pipe pipe)
		{
			return pipe.GetParameter(BuiltInParameter.RBS_PIPE_INNER_DIAM_PARAM).AsDouble() * 304.8;
		}

		private string GetInnerDiameter(Pipe pipe, bool beString)
		{
			return pipe.GetParameter(BuiltInParameter.RBS_PIPE_INNER_DIAM_PARAM).AsValueString();
		}

		private double GetDiameter(Pipe pipe)
		{
			return pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
		}

		private string GetDiameter(Pipe pipe, bool beString)
		{
			return pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsValueString();
		}

		private double GetOffset(Pipe pipe)
		{
			return pipe.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble() * 304.8;
		}

		private string GetOffset(Pipe pipe, bool beString)
		{
			return pipe.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM).AsValueString();
		}

		private double GetAbsoluteLevel(Pipe pipe)
		{
			Parameter parameter = pipe.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM);
			double num = parameter.AsDouble();
			parameter = pipe.ReferenceLevel.GetParameter(BuiltInParameter.LEVEL_ELEV);
			return (num + parameter.AsDouble()) * 304.8;
		}

		private string GetAbsoluteLevel(Pipe pipe, bool beString)
		{
			return pipe.GetParameter("绝对标高").AsValueString();
		}

		private XYZ MuiltyDynamicShow(List<Pipe> listPipe, XYZ markPoint, XYZ vVector)
		{
			double num = 0.0;
			double num2 = 0.0;
			this.GetPipeTagMaxLenght(listPipe, ref num, ref num2);
			Document document = listPipe.First<Pipe>().Document;
			MarkPipeLevelUtility.fontHigtht = 0.8 * num * 100.0 / (double)document.ActiveView.Scale;
			MarkPipeLevelUtility.lineLenght = this.GetWidthByRevitVersion(document, num2 * 100.0 / (double)document.ActiveView.Scale);
			MarkPipeLevelUtility.leadDistance = MarkPipeLevelUtility.GetTansferDistance(this.commonFont.LeadDistance, 30) * 100.0 / (double)document.ActiveView.Scale;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(listPipe.First<Pipe>());
			YJKLine hyLine = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			List<Line> listLine = null;
			DirectionVector directionVector = null;
			using (CreatePipeLevelLinesJin createPipeLevelLinesJin = new CreatePipeLevelLinesJin(this.uiDoc, this.uiDoc.ActiveView, hyLine, false, false, true))
			{
				createPipeLevelLinesJin.selectedElements = listPipe.ToList<Element>();
				createPipeLevelLinesJin.StartPoint = markPoint;
				createPipeLevelLinesJin.mutilDirection = vVector;
				this.selection.PickPoint("选取相对于引线的方向点……");
				directionVector = createPipeLevelLinesJin.directionVector;
				this.dynamicPoint = markPoint;
				this.dynamicVector = directionVector;
				listLine = createPipeLevelLinesJin.resultLines;
			}
			this.DrawMuiltyLines(listLine, document);
			PipeDiameterMarkOperation.PipeTagParamter tagParam = this.LoadPipeTag(listPipe.Last<Pipe>());
			this.CreatMultyPipesTag(listPipe, tagParam, markPoint, directionVector);
			return directionVector.VerticalVector;
		}

		private List<IndependentTag> CreatMultyPipesTag(List<Pipe> listPipe, PipeDiameterMarkOperation.PipeTagParamter tagParam, XYZ basePoint, DirectionVector dirVector)
		{
			Document document = listPipe.First<Pipe>().Document;
			Autodesk.Revit.DB.View activeView = document.ActiveView;
			this.userSetting = new UserSetting();
			List<IndependentTag> list = new List<IndependentTag>();
			CommonFont fontSetting = this.userSetting.GetFontSetting();
			double num = this.GetCreatTagHeight(listPipe.First<Pipe>()) / 304.8;
			double num2 = 0.8 * num;
			double num3 = 0.5 * num2 / 2.0;
			double num4 = MarkPipeLevelUtility.GetTansferDistance(fontSetting.LeadDistance, 30) / 304.8;
			MarkOperation markOperation = new MarkOperation();
			double num5 = (double)document.ActiveView.Scale / 100.0;
			string text = "";
			string text2 = "";
			string tagNameNew = this.GetTagNameNew(ref text, ref text2, listPipe.First<Pipe>());
			num = markOperation.GetDistanceTagToLeader(document, tagNameNew);
			XYZ xyz;
			if (tagParam.PipeVVector.IsAlmostEqualTo(dirVector.VerticalVector.Normalize(), 1E-05))
			{
				xyz = basePoint.Add(num * dirVector.VerticalVector.Normalize());
			}
			else
			{
				xyz = basePoint.Subtract(num * dirVector.VerticalVector.Normalize());
			}
			XYZ horizontalVector = dirVector.HorizontalVector;
			SubTransaction subTransaction = new SubTransaction(document);
			try
			{
				subTransaction.Start();
				for (int i = listPipe.Count - 1; i >= 0; i--)
				{
					PipeDiameterMarkOperation.PipeTagParamter pipeTagParamter = this.LoadPipeTag(listPipe[i]);
					IndependentTag independentTag = RevitVersionFuncs.NewTag(document, activeView, new Reference(listPipe[i]), false, 0, 0, xyz);
					independentTag.ChangeTypeId(pipeTagParamter.FamilySymbol.Id);
					document.Regenerate();
					double creatTagLength = this.GetCreatTagLength(independentTag.TagText, document);
					double num6 = this.GetWidthByRevitVersion(document, creatTagLength) / 2.0 / 304.8;
					new YJKRotateFamilyInstance().MoveElement(num6 * horizontalVector.Normalize(), independentTag);
					list.Add(independentTag);
					xyz = xyz.Add(dirVector.VerticalVector.Normalize() * (num2 + 2.0 * MarkPipeLevelUtility.GetTansferDistance(fontSetting.LeadDistance, 30) / 304.8));
					AssociationMarkInfo.WriteMarkInfoToElement(independentTag, MarkType.PipeDiameter);
					if (MarkPipeLevelUtility.leaderLines.Count == listPipe.Count)
					{
						markOperation.SetAlignStyle(independentTag, new YJKLine(MarkPipeLevelUtility.leaderLines[i]), basePoint, "水管");
					}
				}
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				ex.ToString();
				if (subTransaction != null && (int)subTransaction.GetStatus() == 1)
				{
					subTransaction.RollBack();
				}
				throw;
			}
			return list;
		}

		private void CreatShortLine(XYZ point, Pipe pipe, XYZ vector)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ xyz = new YJKLine(YJKRevitTools.SetZToZero(hypiping.StartConnector.Origin), YJKRevitTools.SetZToZero(hypiping.EndConnector.Origin)).GetProject(point);
			Document document = pipe.Document;
			double num = (double)document.ActiveView.Scale / 100.0;
			double num2 = 0.65616797900262469 * num;
			XYZ xyz2 = xyz.Add(num2 * -1.0 * vector);
			xyz = YJKRevitTools.SetZToZero(xyz);
			xyz2 = YJKRevitTools.SetZToZero(xyz2);
			DetailLine detailLine = YJKRevitTools.DrawDetailLine(document, xyz, xyz2);
			this.allDetailCurve.Insert(detailLine);
		}

		private double GetDistanceTagToPipe(Pipe pipe)
		{
			Document document = pipe.Document;
			Autodesk.Revit.DB.View activeView = document.ActiveView;
			this.userSetting = new UserSetting();
			CommonFont fontSetting = this.userSetting.GetFontSetting();
			fontSetting.TubeDistance = MarkPipeLevelUtility.GetTansferDistance(fontSetting.TubeDistance, 30);
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			int num = document.ActiveView.Scale / 100;
			double num2 = this.GetCreatTagHeight(pipe) / 304.8 * 0.8;
			double result;
			if ((int)activeView.DetailLevel == 3)
			{
				result = hypiping.GetDiameter() / 2.0 + fontSetting.TubeDistance / 304.8 + 0.5 * num2 / 2.0;
			}
			else
			{
				result = fontSetting.TubeDistance / 304.8 + 0.5 * num2 / 2.0;
			}
			return result;
		}

		private double GetDistanceTagToLeader(Autodesk.Revit.DB.Document doc, double tagHeight)
		{
			Autodesk.Revit.DB.View activeView = doc.ActiveView;
			this.userSetting = new UserSetting();
			CommonFont fontSetting = this.userSetting.GetFontSetting();
			fontSetting.LeadDistance = MarkPipeLevelUtility.GetTansferDistance(fontSetting.LeadDistance, 30);
			int num = doc.ActiveView.Scale / 100;
			return fontSetting.LeadDistance / 304.8 + 0.5 * tagHeight / 2.0;
		}

		private Autodesk.Revit.DB.Group CreatGroupLine(ElementSet allDetailCurves, Autodesk.Revit.DB.Document doc)
		{
			if (allDetailCurves == null || allDetailCurves.Size < 1)
			{
				return null;
			}
			Autodesk.Revit.DB.Group group = null;
			SubTransaction subTransaction = new SubTransaction(doc);
			try
			{
				subTransaction.Start();
				group = doc.CreatYJKGroup(allDetailCurves);
				AssociationMarkInfo.WriteMarkInfoToElement(group, MarkType.PipeDiameter);
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				ex.ToString();
				if (!this.diameterMark.BeAutoCancel)
				{
					throw ex;
				}
				this.diameterMark.BeAutoCancel = false;
			}
			return group;
		}

		private void DrawMuiltyLines(List<Line> listLine, Autodesk.Revit.DB.Document doc)
		{
			SubTransaction subTransaction = new SubTransaction(doc);
			try
			{
				subTransaction.Start();
				foreach (Line curve in listLine)
				{
					DetailCurve detailCurve = doc.CreatYJKDetailCurve(curve);
					if (detailCurve != null)
					{
						this.allDetailCurve.Insert(detailCurve);
					}
				}
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				ex.ToString();
				if (!this.diameterMark.BeAutoCancel)
				{
					throw ex;
				}
				this.diameterMark.BeAutoCancel = false;
			}
		}

		private double GetPipeTagMaxLenght(List<Pipe> listPipe, ref double tagLength)
		{
			double num = 10.0;
			string text = "";
			string text2 = "";
			foreach (Pipe pipe in listPipe)
			{
				string tagNameNew = this.GetTagNameNew(ref text, ref text2, pipe);
				double creatTagLength = this.GetCreatTagLength(tagNameNew, pipe.Document);
				tagLength = this.GetTagLength(tagNameNew, pipe.Document);
				if (creatTagLength > num)
				{
					num = creatTagLength;
				}
			}
			return num;
		}

		private void GetPipeTagMaxLenght(List<Pipe> listPipe, ref double tagHeight, ref double tagWidth)
		{
			tagWidth = 10.0;
			string text = "";
			string text2 = "";
			double num = 0.0;
			foreach (Pipe pipe in listPipe)
			{
				num = 0.0;
				string tagNameNew = this.GetTagNameNew(ref text, ref text2, pipe);
				new MarkOperation().GetTagTextWidthAndHeight(tagNameNew, pipe.Document, ref num, ref tagHeight);
				num = num * this.commonFont.LeadRotia * 0.75;
				if (num > tagWidth)
				{
					tagWidth = num;
				}
			}
		}

		private double GetCreatTagLength(string strText, Autodesk.Revit.DB.Document doc)
		{
			double num = 0.0;
			double num2 = 0.0;
			new MarkOperation().GetTagTextWidthAndHeight(strText, doc, ref num2, ref num);
			this.userSetting = new UserSetting();
			CommonFont fontSetting = this.userSetting.GetFontSetting();
			num2 = num2 * fontSetting.LeadRotia * 0.8;
			return num2;
		}

		private double GetTagLength(string strText, Autodesk.Revit.DB.Document doc)
		{
			double num = 0.0;
			double result = 0.0;
			new MarkOperation().GetTagTextWidthAndHeight(strText, doc, ref result, ref num);
			return result;
		}

		private double GetCreatTagHeight(Pipe pipe)
		{
			Document document = pipe.Document;
			string text = "";
			string text2 = "";
			string tagNameNew = this.GetTagNameNew(ref text, ref text2, pipe);
			MarkOperation markOperation = new MarkOperation();
			double num = 0.0;
			double result = 0.0;
			markOperation.GetTagTextWidthAndHeight(tagNameNew, document, ref num, ref result);
			return result;
		}

		private double GetTagTextLenght(string tagText)
		{
			this.userSetting = new UserSetting();
			CommonFont fontSetting = this.userSetting.GetFontSetting();
			return (double)(StringTools.GetStringByteLength(tagText) * 200) * fontSetting.FontGWRotia;
		}

		private List<Pipe> GetPipesByTwoPoints(ref XYZ markPoint, ref XYZ vVector, ref string selectMessage)
		{
			List<Pipe> list = null;
			try
			{
				selectMessage = "";
				IList<Element> list2 = new FilteredElementCollector(this.doc, this.doc.ActiveView.Id).OfClass(typeof(Pipe)).ToElements();
				XYZ xyz = this.selection.PickPoint("请选取第一个点（标注两点连线所穿过的所有管道）......");
				using (DrawLineJin drawLineJin = new DrawLineJin(this.uiDoc))
				{
					drawLineJin.StartPoint = xyz;
					markPoint = this.selection.PickPoint("请选取第二个点......");
					drawLineJin.Dispose();
				}
				list = new List<Pipe>();
				foreach (Element element in list2)
				{
					Pipe pipe = element as Pipe;
					if (!pipe.IsHidden(this.doc.ActiveView) && (!this.UseForVrvLN || this.IsCondensePipe(pipe)))
					{
						LocationCurve locationCurve = pipe.Location as LocationCurve;
						XYZ endPoint = locationCurve.Curve.GetEndPoint(0);
						XYZ endPoint2 = locationCurve.Curve.GetEndPoint(1);
						if (YJKRevitTools.IsLineSegmentCross(xyz, markPoint, endPoint, endPoint2))
						{
							list.Add(pipe);
						}
					}
				}
				if (list == null || list.Count < 1)
				{
					return null;
				}
				list = this.OrderPipesByPickPoint(markPoint, list);
				foreach (Pipe pipe2 in list)
				{
					this.SetPipeParameter(pipe2, markPoint);
				}
				this.userSetting = new UserSetting();
				PipeDiameterMarkSet diameterMarkSetting = this.userSetting.GetDiameterMarkSetting();
				bool beDrawLine = this.CheckStringBeTrue(diameterMarkSetting.DrawSlash);
				this.CreatMarkLine(markPoint, list, beDrawLine, ref vVector);
			}
			catch (Exception ex)
			{
				selectMessage = ex.ToString();
				if (!this.diameterMark.BeAutoCancel)
				{
					throw ex;
				}
				this.diameterMark.BeAutoCancel = false;
			}
			return list;
		}

		private List<Pipe> OrderPipesByPickPoint(XYZ pickPoint, List<Pipe> listPipe)
		{
			List<Pipe> list = new List<Pipe>();
			Dictionary<Pipe, double> dictionary = new Dictionary<Pipe, double>();
			foreach (Pipe pipe in listPipe)
			{
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
				YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				hyline.MakeUnBound();
				XYZ xyz = YJKRevitTools.SetZToZero(hyline.GetProject(pickPoint));
				pickPoint = YJKRevitTools.SetZToZero(pickPoint);
				double value = xyz.DistanceTo(pickPoint);
				dictionary.Add(pipe, value);
			}
			foreach (KeyValuePair<Pipe, double> keyValuePair in from p in dictionary
			orderby p.Value
			select p)
			{
				list.Add(keyValuePair.Key);
			}
			return list;
		}

		private void CreatMarkLine(XYZ markPoint, List<Pipe> listPipe, bool beDrawLine, ref XYZ vVector)
		{
			DetailCurve detailCurve;
			if (beDrawLine)
			{
				foreach (Pipe pipe in listPipe)
				{
					detailCurve = this.CreatSlashLine(markPoint, pipe);
					if (detailCurve != null)
					{
						this.allDetailCurve.Insert(detailCurve);
					}
				}
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(listPipe.Last<Pipe>());
			YJKLine hyline = new YJKLine(YJKRevitTools.SetZToZero(hypiping.StartConnector.Origin), YJKRevitTools.SetZToZero(hypiping.EndConnector.Origin));
			hyline.MakeUnBound();
			XYZ xyz = hyline.GetProject(markPoint);
			markPoint = YJKRevitTools.SetZToZero(markPoint);
			xyz = YJKRevitTools.SetZToZero(xyz);
			vVector = markPoint.Subtract(xyz).Normalize();
			detailCurve = this.CreatDetailLine(markPoint, xyz, this.doc);
			if (beDrawLine)
			{
				this.CreatShortLine(markPoint, listPipe.Last<Pipe>(), vVector);
			}
			if (detailCurve != null)
			{
				this.allDetailCurve.Insert(detailCurve);
			}
		}

		private DetailCurve CreatSlashLine(XYZ markPoint, Pipe pipe)
		{
			XYZ positiveHVector = this.GetPositiveHVector(pipe);
			XYZ positiveVVector = this.GetPositiveVVector(pipe);
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			YJKLine hyline = new YJKLine(YJKRevitTools.SetZToZero(hypiping.StartConnector.Origin), YJKRevitTools.SetZToZero(hypiping.EndConnector.Origin));
			hyline.MakeUnBound();
			XYZ xyz = YJKRevitTools.SetZToZero(hyline.GetProject(markPoint));
			double num = (double)pipe.Document.ActiveView.Scale / 100.0;
			double num2 = 0.49212598425196846 * num;
			XYZ xyz2 = positiveVVector.Add(-1.0 * positiveHVector).Normalize();
			XYZ startPoint = xyz.Add(xyz2 * num2);
			XYZ endPoint = xyz.Add(xyz2 * -num2);
			return this.CreatDetailLine(startPoint, endPoint, this.doc);
		}

		private DetailCurve CreatDetailLine(XYZ startPoint, XYZ endPoint, Autodesk.Revit.DB.Document doc)
		{
			DetailCurve result = null;
			SubTransaction subTransaction = new SubTransaction(doc);
			try
			{
				subTransaction.Start();
				YJKLine hyline = new YJKLine(startPoint, endPoint);
				result = doc.CreatYJKDetailCurve(hyline.Line);
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				ex.ToString();
				if (subTransaction != null && (int)subTransaction.GetStatus() == 1)
				{
					subTransaction.RollBack();
				}
			}
			return result;
		}

		private XYZ GetPositiveHVector(Pipe pipe)
		{
			XYZ xyz = new XYZ();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ xyz2 = YJKRevitTools.SetZToZero(hypiping.StartConnector.Origin);
			XYZ xyz3 = YJKRevitTools.SetZToZero(hypiping.EndConnector.Origin);
			xyz = (xyz3 - xyz2).Normalize();
			XYZ xyz4 = new XYZ(1.0, 0.0, 0.0);
			if (xyz4.AngleTo(xyz) > xyz4.AngleTo(-1.0 * xyz))
			{
				xyz = -1.0 * xyz;
			}
			else if (Math.Abs(xyz4.AngleTo(xyz) - xyz4.AngleTo(-1.0 * xyz)) < 0.0001 && xyz2.Y > xyz3.Y)
			{
				xyz = -1.0 * xyz;
			}
			return xyz;
		}

		private XYZ GetPositiveVVector(Pipe pipe)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ xyz = YJKRevitTools.SetZToZero(hypiping.StartConnector.Origin);
			XYZ xyz2 = YJKRevitTools.SetZToZero(hypiping.EndConnector.Origin);
			XYZ xyz3 = YJKRevitTools.GetVectorByHorizontal((xyz2 - xyz).Normalize());
			XYZ xyz4 = new XYZ(0.0, 1.0, 0.0);
			if (xyz4.AngleTo(xyz3) > xyz4.AngleTo(-1.0 * xyz3))
			{
				xyz3 = -1.0 * xyz3;
			}
			else if (Math.Abs(xyz4.AngleTo(xyz3) - xyz4.AngleTo(-1.0 * xyz3)) < 0.001 && xyz.Y > xyz2.Y)
			{
				xyz3 = -1.0 * xyz3;
			}
			return xyz3;
		}

		private bool IsCondensePipe(Pipe pipe)
		{
			return pipe.LookupParameter("系统类型").AsValueString().Contains("冷凝");
		}

		private ExternalCommandData commandData;

		private UIDocument uiDoc;

		private Document doc;

		private FormPipeDiameterMark diameterMark;

		private Selection selection;

		private UserSetting userSetting;

		private PipeDiameterMarkSet markSet;

		private List<PrefixSetting> allSetting;

		private ElementSet allDetailCurve;

		private XYZ dynamicPoint;

		private DirectionVector dynamicVector;

		private const double HALFTAGHEIGHT = 400.0;

		private CommonFont commonFont;

		private enum MarkSymbol
		{
			SystemAbsolutely,
			SystemRelative,
			Absolutely,
			Relative,
			System,
			Thickness,
			Diameter
		}

		private enum MarkDiameterType
		{
			Diameter,
			OutDiameter,
			InnerDiameter
		}

		private class PipeTagParamter
		{
			public Pipe BasePipe { get; set; }

			public XYZ PickPoint { get; set; }

			public XYZ PipeHVector { get; set; }

			public XYZ PipeVVector { get; set; }

			public XYZ DynamicHVector { get; set; }

			public XYZ DynamicVVector { get; set; }

			public string FamilyName { get; set; }

			public string SymbolName { get; set; }

			public FamilySymbol FamilySymbol { get; set; }
		}
	}
}
